<?php
/**
 * 管理后台-用户管理控制器-RCenter
 * 用于控制搜索用户、增删改用户信息
 * 该程序亦与UCenter进行交互
 * @author Pony
 */
class member extends spController{
	function __construct(){
		if($_SESSION['ucid'] != RC_ADMIN){$this->error('您不是RCenter管理员',spUrl('main','login'));}
		parent::__construct();
	}
	
	function index(){
		$lib = spClass('lib_member');
		$this->tpl_img = TPL_IMG;
		$this -> page = $this->spArgs('page',1);
		$this -> memberlist = $lib -> getMembers($this->spArgs('page',1));
		$this -> memberlist = $lib -> mixUc($this->memberlist);
		$this -> memberlist = $lib -> exGroupId($this->memberlist);
		$this -> pager = $lib -> memberPager();
		$this -> group_options = $lib -> exGroup();
		$this -> display('admin_user.htm');
	}
	
	function search(){
		$lib = spClass('lib_member');
		if($this->spArgs('username') != FALSE){
			$userinfo = spClass('spUcenter') -> uc_get_user($this->spArgs('username'));
			if($userinfo[0] > 0){
				$this -> memberlist = $lib -> getMembers(NULL,$userinfo[0]);
			}
		}
		elseif ($this->spArgs('groupid') != FALSE){
			$this->memberlist = $lib -> getMembers(NULL,NULL,$this->spArgs('groupid'));
		}
		$this -> tpl_img = TPL_IMG;
		$this -> group_options = $lib -> exGroup();
		$this -> memberlist = $lib -> mixUc($this->memberlist);
		$this -> memberlist = $lib -> exGroupId($this->memberlist);
		$this -> display('admin_user.htm');
	}
	
	function delete(){
		$lib = spClass('lib_member');
		$deletes_wait = $this->spArgs('delete');
		while ($foo = each($deletes_wait)){
			$lib -> deleteMember($foo['value']);
		}
		$this->success('已从RCenter中删除选定用户',spUrl('member'));
	}
	
	function edit_page(){
		$lib = spClass('lib_member');
		$this -> memberinfo = $lib -> getMembers(NULL,$this->spArgs('ucid'));
		$this -> userinfo = spClass('spUcenter') -> uc_get_user($this -> memberinfo['ucid'], 1);
		$this -> tpl_img = TPL_IMG;
		$this -> group_options = $lib -> exGroup();
		$this -> display('admin_user_edit.htm');
	}
	
	function edit(){
		$uc = spClass('spUcenter') -> uc_user_edit($this->spArgs('username'), NULL, $this->spArgs('password'), $this->spArgs('email'), 1);
		$lib = spClass('lib_member');
		$lib -> editMember($this->spArgs('ucid'),$this->spArgs('groupid'));
		$this->success('修改完成',spUrl('member','edit_page',array('ucid'=>$this->spArgs('ucid'))));
	}
	
	function add(){
		if($this->spArgs('username') == FALSE)$this->error('用户名不可为空',spUrl('main','register'));
		$lib = spClass('lib_member');
		$Ucenter = spClass('spUcenter');
		//注册，并获取UCenterID
		$result = $Ucenter -> uc_user_register($this->spArgs('username'),$this->spArgs('password'),$this->spArgs('email'));
		
		if($result > 0){
			$Rcenter = $lib -> addUser($result,$this->spArgs('groupid'));
			$this->success('已成功添加',spUrl('main','login'));
		}
		elseif($result == '-3'){
			$uid = $Ucenter -> uc_user_login($this->spArgs('username'),$this->spArgs('password'));
			$Rcenter = $lib -> addUser($uid[0],$this->spArgs('groupid'));
			$this->success('已成功添加',spUrl('main','login'));
		}
		elseif ($result <= -4) {
			$this->error('请更改一个邮箱',spUrl('main','register'));
		}
	}
}